import akshare as ak
import matplotlib.pyplot as plt

stock= '603860'
# 获取stock的股票数据，日期范围从 2020-01-01 到 2021-01-01
stock_data_cleaned = ak.stock_zh_a_hist(stock,period= "daily", start_date="20240101" , end_date='20241218')

# 计算 RSI 指标
delta = stock_data_cleaned['收盘'].diff(1)
gain = delta.where(delta > 0, 0)
loss = -delta.where(delta < 0, 0)

# 计算平均收益和损失
avg_gain = gain.rolling(window=14).mean()
avg_loss = loss.rolling(window=14).mean()

# 计算相对强弱指数 RSI
rs = avg_gain / avg_loss
rsi = 100 - (100 / (1 + rs))

# 添加 RSI 到数据中
stock_data_cleaned['RSI'] = rsi

# 绘制 RSI 曲线
plt.figure(figsize=(12, 6))
plt.plot(stock_data_cleaned['RSI'], label='RSI')
plt.axhline(y=70, color='r', linestyle='--', label='Overbought (70)')
plt.axhline(y=30, color='g', linestyle='--', label='Oversold (30)')
plt.title('RSI Indicator for Maotai Stock', fontsize=14)
plt.xlabel('Date', fontsize=12)
plt.ylabel('RSI', fontsize=12)
plt.legend()
plt.grid(True)
plt.show()